# -*- coding: utf-8 -*-

import json
import os
import requests

jenkins_url = "https://jenkins.dingrennet.cn/jenkins/"
user = "admin"
password = "Drhz123!@#"

def get_all_project_info():
    # Jenkins 地址
    url = jenkins_url + "/api/json"
    print(url)
    r = requests.get(url, auth=(user, password))
    job_infos = r.json()['jobs']
    for proj in job_infos:
        print(proj['name'], proj['url'])
        get_job_info(proj['name'], proj['url'])


def get_job_info(job_name, job_url):
    print(job_url)
    job_url = job_url.replace("https://jenkins.dingrennet.cn/","https://jenkins.dingrennet.cn/jenkins/")
    url = job_url + "config.xml"
    print(url)
    conf_dir = "config"
    if not os.path.exists(conf_dir):
        os.mkdir(conf_dir)
    job_name = conf_dir + "/" + job_name + ".xml"
    r = requests.get(url, auth = (user, password))
    with open(job_name, 'w', encoding='utf-8') as f:
        f.write(r.text)


def main():
    """ Jenkin API
    1、获取所有项目信息
    2、获取每个项目的信息
    """
    # 获取所有项目信息
    get_all_project_info()
    pass


if __name__ == "__main__":
    print("application begin")
    main()